<?php

class UserManager extends TModule implements IUserManager
{
	// -------------------- basic functions --------------------
	public function addUser($login, $password, $first_name, $last_name, $email, $registration)
	{
		$user = new UserRecord();
		$user->setData(	0, $login, $password, $first_name, $last_name, $email, $registration);
		
		if($user->save())
		{
			// add default participant role
			$role_manager = $this->Application->Modules['roles'];
			$role = $role_manager->getRoleByTitle(RoleManager::$PARTICIPANT_ROLE);
			if($role)
			{
				$user_role_manager = $this->Application->Modules['users_roles'];
				$user_role_manager->addUserRole($user->getId(), $role->getId());
			}
			
			return $user;
		}
		else
			return null;
	}
	
	public function deleteUser($id)
	{
		// delete from roles
		//$user_role_manager = $this->Application->Modules['users_roles'];
		//$user_role_manager->deleteUserRoleByUserId($id);
		
		return UserRecord::finder()->deleteById($id);
	}
	
	public function updateUser($id, $login, $password, $first_name, $last_name, $email, $registration)
	{	
		$userRecord = new UserRecord();
		$userRecord->setData($id, $login, $password, $first_name, $last_name, $email, $registration);
		return $this->updateUserByUserRecord($userRecord);
	}

	public function updateUserByUserRecord($userRecord)
	{	
		if($userRecord instanceof UserRecord)
		{
			$finder = UserRecord::finder();
			$id = $userRecord->getId();
			if($id > 0 && $rec = $finder->findByPk($id))
			{
				if($rec->equals($userRecord))
					return $rec;
				$rec->setDataByUserRecord($userRecord);
				if($rec->save())
					return $rec;
			}
		}
		return null;
	}
	
	public function getUserById($id)
	{
		return UserRecord::finder()->findByPk($id);
	}
	
	public function getUserByLogin($login)
	{
		return UserRecord::finder()->findByLogin($login);
	}
	
	// -------------------- extended functions --------------------
	//@param integer $revert
	//@param integer $offset
	//@param integer $limit
	/**
	 * @return UserRecord[]
	 * @soapmethod
	 */
	public function getUsersList($revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		
		if($revert)
			$criteria->OrdersBy['registration'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countUsers();
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return UserRecord::finder()->findAll($criteria);
	}
	
	public function countUsers()
	{
		return UserRecord::finder()->count();
	}
	
	public function isLoginReserved($login)
	{
		$reserved = UserRecord::finder()->findByLogin($login);

		if($reserved)
			return true;
		return false;
	}
	
	// ------------------------------------------------------------------------------------------------------------
	// authorization settings
	// ------------------------------------------------------------------------------------------------------------
	public function getUser($login=null)
	{
		if($login === null)
			return new User($this);
		else
		{
			$manager = $this->Application->Modules['users'];
			if(($userRecord = $this->getUserByLogin($login)) !== null)
			{
				$user = new User($this);
				$user->setUserRecord($userRecord);

				$user->setName($userRecord->id);
				$user->setIsGuest(false);
				
				// roles
				$user_role_manager = $this->Application->Modules['users_roles'];
				$roles = $user_role_manager->getUserRolesTitlesListByUserId($userRecord->getId());
				
				// check if is admin
				$role_manager = $this->Application->Modules['roles'];
				if($userRecord->getLogin() == RoleManager::$ADMIN_ROLE)
					$roles[] = RoleManager::$ADMIN_ROLE;

				$user->setRoles($roles);
				
				return $user;
			}
			else
				return null;
		}
	}
	
	public function validateUser($login, $password)
	{
		if(($userRecord = $this->getUserByLogin($login))!==null)
			return $userRecord->getPassword() === $password;
		else
			return false;
	}
	
	public function getGuestName()
	{
		return 'Guest';
	}

	public function saveUserToCookie($cookie)
	{
		// do nothing since we don't support cookie-based auth in this example
	}
	
	public function getUserFromCookie($cookie)
	{
		// do nothing since we don't support cookie-based auth in this example
		return null;
	}
}

?>